package com.bang.concurrency.example.aqs;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Auther: Bang
 * @Date: 2019/10/3 22:24
 * @Description:
 */
@Slf4j
public class CountDownLatchExample1 {

    // 请求数
    private static final int clientCount = 5000;

    public static void main(String[] args) throws InterruptedException {
        ExecutorService executorService = Executors.newCachedThreadPool();
        CountDownLatch countDownLatch = new CountDownLatch(clientCount);
        for (int i = 0; i <clientCount ; i++) {
            final int count = i;
            executorService.execute(()->{
                try {
                    test(count);
                } catch (Exception e) {
                    log.error("e,", e);
                }finally {
                    countDownLatch.countDown();
                }

            });
        }
        countDownLatch.await();
        executorService.shutdown();
        log.error("finish");

    }

    private static void test(int count) throws InterruptedException {
        Thread.sleep(100);
        log.error("exec --> ", count);

    }
}
